package com.qianfan.qfcall.network.manager

import com.hjq.gson.factory.GsonFactory
import com.qianfan.qfcall.network.constant.*
import com.wxq.workpad.network.interceptor.QfInterceptor

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object HttpManager {

    private var retrofit: Retrofit

    init {

        retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(GsonFactory.getSingletonGson()))
            .client(initClient())
            .build()
    }

    fun updateBaseUrl(url:String){
        retrofit = Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create(GsonFactory.getSingletonGson()))
            .client(initClient())
            .build()
    }



    private fun initClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(QfInterceptor())
            .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
            .readTimeout(TIME_OUT, TimeUnit.SECONDS)
            .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
            .build()
    }

    fun <T> create(service: Class<T>): T {
        return retrofit.create(service)
    }

}